<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="设备识别码" prop="equId">
      <el-input v-model="dataForm.equId" placeholder="设备识别码"></el-input>
    </el-form-item>
    <el-form-item label="设备运行时间戳" prop="runTime">
      <el-input v-model="dataForm.runTime" placeholder="设备运行时间戳"></el-input>
    </el-form-item>
    <el-form-item label="经度" prop="longitude">
      <el-input v-model="dataForm.longitude" placeholder="经度"></el-input>
    </el-form-item>
    <el-form-item label="纬度" prop="latitude">
      <el-input v-model="dataForm.latitude" placeholder="纬度"></el-input>
    </el-form-item>
    <el-form-item label="设备状态" prop="equStatus">
      <el-input v-model="dataForm.equStatus" placeholder="设备状态"></el-input>
    </el-form-item>
    <el-form-item label="设备步数" prop="equSteps">
      <el-input v-model="dataForm.equSteps" placeholder="设备步数"></el-input>
    </el-form-item>
    <el-form-item label="设备电量" prop="equElectric">
      <el-input v-model="dataForm.equElectric" placeholder="设备电量"></el-input>
    </el-form-item>
    <el-form-item label="设备温度" prop="equTemperature">
      <el-input v-model="dataForm.equTemperature" placeholder="设备温度"></el-input>
    </el-form-item>
    <el-form-item label="设备电压" prop="equVoltage">
      <el-input v-model="dataForm.equVoltage" placeholder="设备电压"></el-input>
    </el-form-item>
    <el-form-item label="电流" prop="equCurrent">
      <el-input v-model="dataForm.equCurrent" placeholder="电流"></el-input>
    </el-form-item>
    <el-form-item label="不定长数据" prop="variableLength">
      <el-input v-model="dataForm.variableLength" placeholder="不定长数据"></el-input>
    </el-form-item>
    <el-form-item label="创建时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="创建时间"></el-input>
    </el-form-item>
    <el-form-item label="所有报文信息" prop="allMessage">
      <el-input v-model="dataForm.allMessage" placeholder="所有报文信息"></el-input>
    </el-form-item>
    <el-form-item label="表名" prop="tableName">
      <el-input v-model="dataForm.tableName" placeholder="表名"></el-input>
    </el-form-item>
    <el-form-item label="%6000余数" prop="remainder">
      <el-input v-model="dataForm.remainder" placeholder="%6000余数"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          equId: '',
          runTime: '',
          longitude: '',
          latitude: '',
          equStatus: '',
          equSteps: '',
          equElectric: '',
          equTemperature: '',
          equVoltage: '',
          equCurrent: '',
          variableLength: '',
          createTime: '',
          allMessage: '',
          tableName: '',
          remainder: ''
        },
        dataRule: {
          equId: [
            { required: true, message: '设备识别码不能为空', trigger: 'blur' }
          ],
          runTime: [
            { required: true, message: '设备运行时间戳不能为空', trigger: 'blur' }
          ],
          longitude: [
            { required: true, message: '经度不能为空', trigger: 'blur' }
          ],
          latitude: [
            { required: true, message: '纬度不能为空', trigger: 'blur' }
          ],
          equStatus: [
            { required: true, message: '设备状态不能为空', trigger: 'blur' }
          ],
          equSteps: [
            { required: true, message: '设备步数不能为空', trigger: 'blur' }
          ],
          equElectric: [
            { required: true, message: '设备电量不能为空', trigger: 'blur' }
          ],
          equTemperature: [
            { required: true, message: '设备温度不能为空', trigger: 'blur' }
          ],
          equVoltage: [
            { required: true, message: '设备电压不能为空', trigger: 'blur' }
          ],
          equCurrent: [
            { required: true, message: '电流不能为空', trigger: 'blur' }
          ],
          variableLength: [
            { required: true, message: '不定长数据不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          allMessage: [
            { required: true, message: '所有报文信息不能为空', trigger: 'blur' }
          ],
          tableName: [
            { required: true, message: '表名不能为空', trigger: 'blur' }
          ],
          remainder: [
            { required: true, message: '%6000余数不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/generator/message1020321081613202108/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.equId = data.message1020321081613202108.equId
                this.dataForm.runTime = data.message1020321081613202108.runTime
                this.dataForm.longitude = data.message1020321081613202108.longitude
                this.dataForm.latitude = data.message1020321081613202108.latitude
                this.dataForm.equStatus = data.message1020321081613202108.equStatus
                this.dataForm.equSteps = data.message1020321081613202108.equSteps
                this.dataForm.equElectric = data.message1020321081613202108.equElectric
                this.dataForm.equTemperature = data.message1020321081613202108.equTemperature
                this.dataForm.equVoltage = data.message1020321081613202108.equVoltage
                this.dataForm.equCurrent = data.message1020321081613202108.equCurrent
                this.dataForm.variableLength = data.message1020321081613202108.variableLength
                this.dataForm.createTime = data.message1020321081613202108.createTime
                this.dataForm.allMessage = data.message1020321081613202108.allMessage
                this.dataForm.tableName = data.message1020321081613202108.tableName
                this.dataForm.remainder = data.message1020321081613202108.remainder
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/message1020321081613202108/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'equId': this.dataForm.equId,
                'runTime': this.dataForm.runTime,
                'longitude': this.dataForm.longitude,
                'latitude': this.dataForm.latitude,
                'equStatus': this.dataForm.equStatus,
                'equSteps': this.dataForm.equSteps,
                'equElectric': this.dataForm.equElectric,
                'equTemperature': this.dataForm.equTemperature,
                'equVoltage': this.dataForm.equVoltage,
                'equCurrent': this.dataForm.equCurrent,
                'variableLength': this.dataForm.variableLength,
                'createTime': this.dataForm.createTime,
                'allMessage': this.dataForm.allMessage,
                'tableName': this.dataForm.tableName,
                'remainder': this.dataForm.remainder
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
